import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter_desktop_alert/flutter_desktop_alert.dart';
import 'package:flutter_desktop_alert_example/toast_utils.dart';

void main() {
  runApp(const MyApp());
}

String _shareCount = "";
String _money = "";

String _shareCount2 = "";
String _money2 = "";

TextEditingController firstEC = TextEditingController();
FocusNode firstNode = FocusNode();

TextEditingController secondEC = TextEditingController();
FocusNode secondNode = FocusNode();

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';

  @override
  void initState() {
    super.initState();
    initPlatformState();

    // 初始化dart层类(一定要记得)，先监听
    FlutterDesktopAlert flugin = FlutterDesktopAlert();
    FlutterDesktopAlert.listener((args, args2,agrs3) {
      print("args=$args-----args2=$args2");
      if(args == "initCall"){
        setState(() {
          _shareCount = args2;
          _money = agrs3;
        });
      }
      else if(args == "updateCall") {
        setState(() {
          _shareCount2 = args2;
          _money2 = agrs3;
        });
      }

    });
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      platformVersion =
          await FlutterDesktopAlert.platformVersion ?? 'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }


  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text('Running on: $_platformVersion\n'),
              Container(
                margin: const EdgeInsets.all(10),
                padding: const EdgeInsets.all(10),
                width: 200,
                decoration: BoxDecoration(
                  color: Colors.red,
                  borderRadius: BorderRadius.circular(10),
                ),
                alignment: Alignment.center,
                child: InkWell(
                  onTap: () async {
                    print("初始化插件");
                    String? clickStr;
                    clickStr = await FlutterDesktopAlert.creatDesktopWidget("100.0", "10");
                  },
                  child: Text("初始化插件"),
                ),
              ),
              Text('初始化值---分享次数: $_shareCount----金额$_money'),
              Container(
                // width: 300,
                height: 50,
                child: Row(
                  children: [
                    const SizedBox(width: 40,),
                    Expanded(child: TextField(
                      controller: firstEC,
                      focusNode: firstNode,
                      keyboardType: TextInputType.number,
                      style:
                      const TextStyle(fontSize: 13.0, color: Colors.black),
                      decoration: const InputDecoration(
                        border: InputBorder.none,
                        hintText: "请输入分享次数",
                        hintStyle:
                        TextStyle(fontSize: 13.0, color: Colors.grey),
                      ),


                    ),),
                    const SizedBox(width: 40,),
                  ],
                ),
              ),

              Container(
                // width: MediaQuery.of(context).size.width * 0.5,
                height: 50,
                child: Row(
                  children: [
                    const SizedBox(width: 40,),
                    Expanded(child: TextField(
                      controller: secondEC,
                      focusNode: secondNode,
                      keyboardType: TextInputType.number,
                      style:
                      const TextStyle(fontSize: 13.0, color: Colors.black),
                      decoration: const InputDecoration(
                        border: InputBorder.none,
                        hintText: "请输入获取金额",
                        hintStyle:
                        TextStyle(fontSize: 13.0, color: Colors.grey),
                      ),


                    ),),
                    const SizedBox(width: 40,),
                  ],
                ),
              ),

              Container(
                width: 200,
                margin: const EdgeInsets.all(10),
                padding: const EdgeInsets.all(10),
                decoration: BoxDecoration(
                  color: Colors.red,
                  borderRadius: BorderRadius.circular(10),
                ),
                alignment: Alignment.center,
                child: InkWell(
                  onTap: () async {
                    print("更新插件数据");
                    String? clickStr;
                    if(firstEC.text.isNotEmpty && secondEC.text.isNotEmpty) {
                      clickStr = await FlutterDesktopAlert.updateDesktopWidget(firstEC.text, secondEC.text);
                    }
                    else {
                      ToastUtils.showToast("请输入内容");
                    }

                  },
                  child: Text("更新插件数据"),
                ),
              ),
              Text('更新插件数据---分享次数: $_shareCount2----金额$_money2'),
            ],
          ),
        ),
      ),
    );
  }
}
